home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / battutor.arc / _FIND.REM < prev    next >
Text File  |  1986-05-05  |  8KB  |  204 lines

  1.                             Text File      _find.rem
  2.  
  3.  
  4.  
  5.                          FINDFILE.COM  and  SETDISK.COM
  6.  
  7.  
  8. A desirable feature of a "friendly" Batch File is the ability of the Batch File
  9. to "orient" itself.  By this we mean the ability to discover on which drive are
  10. located useful files such as system utilities or files which the running Batch
  11. File would like to either run or display.
  12.  
  13. We can use searching algorithms involving DOS's PATH command and IF EXIST Batch
  14. File subcommand, but these commands have a drawback.  When a PATH search or an
  15. IF EXIST test on a directory is done on a drive that is not ready (i.e., door
  16. open), the program is interrupted with the critical error message (the famous
  17. "Abort, Retry, or Ignore" message).  We would like to be more unobtrusive in
  18. our searching, allowing the user as much freedom as possible about the use (and
  19. intentional non-use) of his disk drives.  If we are going to bother the user
  20. with as few questions, prompts, and constraints as possible and still find what
  21. we need, then we would like a more benign method of searching for files; that
  22. is, a searching utility that will just search the "ready" drives, passing over
  23. any drives that are not ready.  In that way, we only need to bother the user
  24. with a polite message if, after looking everywhere that we can, we still cannot
  25. find the file we need.
  26.  
  27. We introduce two utilities to implement this ability to orient a Batch File,
  28. and then we give a couple of examples of their use.  Again, these utilities
  29. have been used previously in this tutorial, so listings of these Batch Files
  30. are also a source of examples.
  31.  
  32.  
  33.                                    FINDFILE
  34.  
  35. The purpose of FINDFILE is to accept a file name on its command line and then
  36. search all existing physical drives for the file, bypassing drives that are not
  37. ready for any reason, until it finds the first drive which contains the given
  38. file name.
  39.  
  40. The syntax of the command line is as follows:
  41.  
  42.                 FINDFILE      fname
  43.  
  44. where  "fname"  is a filename and extension.  Giving a disk drive specification
  45. with fname (i.e., "A:fname") is paradoxical, and so a disk drive spec in front
  46. of fname is ignored.  FINDFILE searches for the file and reports its findings
  47. in the errorlevel parameter:
  48.  
  49.  
  50.         errorlevel = 0          if fname is found on A:
  51.  
  52.         errorlevel = 1          if fname is found on B:
  53.  
  54.         errorlevel = 2          if fname is found on C:
  55.  
  56.         errorlevel = 3          if fname is found on D:
  57.                                 .
  58.                                 .
  59.                                 .
  60.                                etc
  61.  
  62.         errorlevel = 255        if fname is not found
  63.  
  64.         errorlevel = 255, and
  65.         CTRL BREAK is executed if fname is missing from command line
  66.  
  67.  
  68. Notice that leaving off the fname is assumed to be a bug during the development
  69. of the Batch File, so a CTRL BREAK is executed to allow the Batch File to be
  70. aborted if desired.
  71.  
  72.  
  73.  
  74.  
  75.  
  76.                                     SETDISK
  77.  
  78.  
  79. SETDISK has the same syntax as FINDFILE:
  80.  
  81.                 SETDISK    fname
  82.  
  83. The operation of SETDISK is the same as FINDFILE with one additional feature.
  84. If SETDISK finds the fname, it not only reports the disk number in errorlevel,
  85. but it also automatically sets the default drive designator to the drive that
  86. contains the file.
  87.  
  88.  
  89. Lets give a couple of examples of the use of these utilities.
  90.  
  91.  
  92.                                     Pathing
  93.  
  94.  
  95. Suppose at some point in our Batch File we want to display a text file called
  96. COMMENTS.REM.  We would like to use the DOS command MORE because of its
  97. automatic pauses when the screen is full ( is this beginning to sound
  98. familiar?). We need to find where the MORE.COM file is residing, but the only
  99. constraint that we want to place on the user is to have both the  MORE.COM file
  100. and the COMMENTS.REM file available (in the current directory ) on any disk
  101. drives.  So, after a friendly prompt asking the user to make the files
  102. available on any drives, we execute the following command lines in the Batch
  103. File:
  104.  
  105.  
  106.                 findfile  more.com
  107.                 if errorlevel 255 goto cantfind
  108.                 if not errorlevel 4  path = d:\
  109.                 if not errorlevel 3  path = c:\
  110.                 if not errorlevel 2  path = b:\
  111.                 if not errorlevel 1  path = a:\
  112.                 setdisk  comments.rem
  113.                 if errorlevel 255 goto cantfind
  114.                 more <comments.rem
  115.  
  116.  
  117. The testing after FINDFILE will leave the PATH environment pointing to the
  118. drive containing MORE.COM.  DOS will follow that "path" to MORE.COM if it can't
  119. find it on the default drive when the MORE command line is executed.  Then
  120. SETDISK will set the default drive to the drive containing the COMMENTS.REM
  121. file, so that the references to COMMENTS.REM in the Batch File commands need
  122. not have a fixed, explicit drive reference.
  123.  
  124.  
  125.                                   Where am I?
  126.  
  127.  
  128. If we have a very large Batch File function that is broken up into many .BAT
  129. files, text comment files, and utilities ( as we do right here ),then we would
  130. like to do one very important orientation of the Batch File right at the
  131. beginning. We would like to set the default drive to the drive that the Batch
  132. File itself is running on, so that all our references to companion files (which
  133. we assume will be on the same diskette) can be written without drive
  134. specifiers, allowing the Batch File to run in any drive.
  135.  
  136. We might just execute something like the following...
  137.  
  138.                 SETDISK   runme.bat
  139.  
  140. at the beginning of the runme.bat Batch File. This will work if the SETDISK.COM
  141. file is already located on the default drive.  But if SETDISK.COM is one of the
  142. utilities on this "load it anywhere" diskette, then we have a problem.  We must
  143. either constrain the user to run the Batch File with the default disk
  144. designator pointing to it, as we did with this tutorial, or we must do a
  145. "fixed" path search for the drive with "ourself" on it, and then change the
  146. default drive designator to that drive.  However, we cannot pick a general
  147. fixed path for all systems.  For instance, if we chose the following path to
  148. try to find SETDISK.COM ...
  149.  
  150.                 path a:\; b:\; c:\; d:\
  151.                 setdisk  runme.bat
  152.  
  153. then DOS's path search would blow up with the critical error message if
  154. SETDISK.COM were located on drive C: and drive A: or B: had its door open
  155. (plus, on a single-drive XT, we would also get the "Insert the diskette for
  156. drive A:/B:" messages ).  Clearly, there are problems with these two utilities
  157. when used in a drive configuration that is unknown at the time that the Batch
  158. File is created.
  159.  
  160. When using these utilities for your own "in house" Batch Files, you can adopt
  161. conventions such as locating all the utilities on a certain disk drive, or,
  162. better yet, copying the utilities to a RAM disk as part of your AUTOEXEC
  163. warm-start sequence, and then always running Batch Files on the RAM disk for
  164. enhanced performance.
  165.  
  166. If these two utilities are more confusing and upsetting than helpful, then
  167. forget them and just constrain the user ( who is probably yourself -- "we have
  168. seen the enemy, and he is us" ) to use certain drives and to run Batch Files
  169. with the default drive designator always pointing to the Batch File disk.
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187. Let's demonstrate the "benign" search feature.  We'll search for the disk with
  188. MORE.COM on it.  If convenient, try leaving a drive door open on the drive that
  189. has MORE.COM on it, or a drive before it. Don't open the door of the drive
  190. containing the Batch File, and ...
  191.  
  192.         DON'T OPEN OR CLOSE A DRIVE DOOR WHILE THE DRIVE LIGHT IS ON!
  193.  
  194. Do it during the "pause" AFTER I say it's OK to open a drive door.
  195.  
  196. Be patient when the search arrives at a not-ready drive; the light will stay on
  197. for about 15 seconds (I hope to reduce this time in a subsequent version, but
  198. it's tricky ...), but it will eventually time-out and then continue the
  199. search.
  200.  
  201. Here's the pause ...
  202. l you get the
  203. hang of it, then restar